

% Properties 
colWidCM = [85, 114, 174]/10; % in mm
% Markup properties (smaller for insets!)
fontSize = 7;
lineWidth = 0.5;
markerSize = 8;
% Sizes
axAspectRatio = 10.5/13;
axWidth =colWidCM(3)/4;
axHeight= colWidCM(3)/4*axAspectRatio;


dpath = '/Users/landauland/Documents/Research/SabatiniLab/data/manuscriptPreparation/compileData';
allData = load(fullfile(dpath,'figure7'));
dataRedo = allData.dataRedo;
kaDensityAll = dataRedo.matKaDensity(:,1);
silentIDAll = dataRedo.silentID;
cutExpAll = dataRedo.matCutExp;

dataSame = allData.dataSame;
tv = dataSame.matTV;
idxROI = dataSame.matIdxROI;
idxActFit = idxROI~=0;
idxROI_ActFit = idxROI(idxActFit);
vTracesActFit = dataSame.matVoltage(:,idxActFit,:);
cTracesActFit = dataSame.matCalcium(:,idxActFit,:);
apAmpActFit = dataSame.matApAmp(idxActFit,:);
caAmpActFit = dataSame.matCaAmp(idxActFit,:);
iresActFit = dataSame.matIres(idxActFit,:);
epspDendActFit = dataSame.epspAmpDend(idxActFit,:);
epspSomaActFit = dataSame.epspAmpSoma(idxActFit,:);
cellID_ActFit = dataSame.matCellID(idxActFit);
cutExp_ActFit = dataSame.matCutExp(idxActFit);

tvEpsp = dataSame.tvEpsp(1,:);
vEpspDendActFit = dataSame.epspDendTraces(:,idxActFit,:);
vEpspSomaActFit = dataSame.epspSomaTraces(:,idxActFit,:);

NT = size(vTracesActFit,1);
numFits = size(vTracesActFit, 2)/2;

maxROI = size(vTracesActFit, 3);

actExVoltage = zeros(NT,numFits,2);
silExVoltage = zeros(NT,numFits,2);
silExVoltage(:,:,1) = vTracesActFit(:,cutExp_ActFit==0,1);
silExVoltage(:,:,2) = vTracesActFit(:,cutExp_ActFit==1,1);
actExCalcium = zeros(NT,numFits,2);
silExCalcium = zeros(NT,numFits,2);
silExCalcium(:,:,1) = cTracesActFit(:,cutExp_ActFit==0,1);
silExCalcium(:,:,2) = cTracesActFit(:,cutExp_ActFit==1,1);
exApAmp = zeros(2,numFits,2);
exCaAmp = zeros(2,numFits,2);
exIres = zeros(2,numFits,2);
exEpspDend = zeros(2,numFits,2);
exEpspSoma = zeros(2,numFits,2);
exApAmp(1,:,1) = apAmpActFit(cutExp_ActFit==0,1);
exCaAmp(1,:,1) = caAmpActFit(cutExp_ActFit==0,1);
exIres(1,:,1) = iresActFit(cutExp_ActFit==0,1);
exEpspDend(1,:,1) = epspDendActFit(cutExp_ActFit==0,1);
exEpspSoma(1,:,1) = epspSomaActFit(cutExp_ActFit==0,1);
exApAmp(1,:,2) = apAmpActFit(cutExp_ActFit==1,1);
exCaAmp(1,:,2) = caAmpActFit(cutExp_ActFit==1,1);
exIres(1,:,2) = iresActFit(cutExp_ActFit==1,1);
exEpspDend(1,:,2) = epspDendActFit(cutExp_ActFit==1,1);
exEpspSoma(1,:,2) = epspSomaActFit(cutExp_ActFit==1,1);
epspNT = length(tvEpsp);
actExEpspDend = zeros(epspNT,numFits,2);
silExEpspDend = zeros(epspNT,numFits,2);
actExEpspSoma = zeros(epspNT,numFits,2);
silExEpspSoma = zeros(epspNT,numFits,2);
silExEpspDend(:,:,1) = vEpspDendActFit(:,cutExp_ActFit==0,1);
silExEpspDend(:,:,2) = vEpspDendActFit(:,cutExp_ActFit==1,1);
silExEpspSoma(:,:,1) = vEpspSomaActFit(:,cutExp_ActFit==0,1);
silExEpspSoma(:,:,2) = vEpspSomaActFit(:,cutExp_ActFit==1,1);

idxNoCut = find(cutExp_ActFit==0);
idxCutExp = find(cutExp_ActFit);
for nf = 1:numFits
    actExVoltage(:,nf,1) = vTracesActFit(:,idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    actExCalcium(:,nf,1) = cTracesActFit(:,idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    exApAmp(2,nf,1) = apAmpActFit(idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    exCaAmp(2,nf,1) = caAmpActFit(idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    exIres(2,nf,1) = iresActFit(idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    exEpspDend(2,nf,1) = actExEpspDend(idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    exEpspSoma(2,nf,1) = actExEpspSoma(idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    
    actExEpspDend(:,nf,1) = vEpspDendActFit(:,idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
    actExEpspSoma(:,nf,1) = vEpspSomaActFit(:,idxNoCut(nf),idxROI_ActFit(idxNoCut(nf))+1);
end
for nf = 1:numFits
    actExVoltage(:,nf,2) = vTracesActFit(:,idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    actExCalcium(:,nf,2) = cTracesActFit(:,idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    exApAmp(2,nf,2) = apAmpActFit(idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    exCaAmp(2,nf,2) = caAmpActFit(idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    exIres(2,nf,2) = iresActFit(idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    exEpspDend(2,nf,2) = actExEpspDend(idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    exEpspSoma(2,nf,2) = actExEpspSoma(idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    
    actExEpspDend(:,nf,2) = vEpspDendActFit(:,idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
    actExEpspSoma(:,nf,2) = vEpspSomaActFit(:,idxCutExp(nf),idxROI_ActFit(idxCutExp(nf))+1);
end


mzpFigureWidth = 4.12;
mzpHeight = 3.4; 

exCell = 1;
exROI = 1;
idxCell = find(cellID_ActFit==exCell);
idxUse = idxCell((0:1) + 2*(exROI-1)+1); % First will be intact, second is cut

tvEndTime = 10;
tvOffset = tvEndTime+1;
tvEndSample = find(tv>=tvEndTime,1);
tvUse = tv(1:tvEndSample);

vcWidth = 11.4 - mzpFigureWidth;
vcHeight = mzpHeight/2;


% Figure 7B
figure(2); clf; hold on; 
set(gcf,'units','centimeters','position',[45 35 vcWidth vcHeight]);
set(gca,'units','centimeters','position',[0.1 0.05 vcWidth*0.93 vcHeight]);
plot(tvUse, vTracesActFit(1:tvEndSample,idxUse(1),exROI+1), 'color','k','linewidth',lineWidth);
plot(tvUse+tvOffset, vTracesActFit(1:tvEndSample,idxUse(2),exROI+1), 'color',0*[1,1,1],'linewidth',lineWidth);
plot(tvUse, vTracesActFit(1:tvEndSample,idxUse(1),1), 'color','b','linewidth',lineWidth);
plot(tvUse+tvOffset, vTracesActFit(1:tvEndSample,idxUse(2),1), 'color',[0.65,0,0],'linewidth',lineWidth);
xlim([0 21]);
ylim([-87 25]);
% Scale Bar
scalePosition = [18 -55 2 10];
line(scalePosition(1)+[0 scalePosition(3)],scalePosition(2)*[1,1],'linewidth',lineWidth,'color','k');
line(scalePosition(1)*[1,1],scalePosition(2)+[0 scalePosition(4)],'linewidth',lineWidth,'color','k');
text(scalePosition(1),scalePosition(2),sprintf('%d ms',scalePosition(3)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(scalePosition(1),scalePosition(2),sprintf('%d mV',scalePosition(4)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
% Mark AP Times
line([1 1],[-85 -80],'color','k','linewidth',lineWidth);
line([1 1]+tvOffset,[-85 -80],'color','k','linewidth',lineWidth);
% Label
text(1,-71,'voltage','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom');
set(gca,'visible','off');


% Figure 7C
figure(3); clf; hold on;
set(gcf,'units','centimeters','position',[45 30 vcWidth vcHeight]);
set(gca,'units','centimeters','position',[0.1 0.05 vcWidth*0.93 vcHeight]);
plot(tvUse, cTracesActFit(1:tvEndSample,idxUse(1),exROI+1), 'color','k','linewidth',lineWidth);
plot(tvUse+tvOffset, cTracesActFit(1:tvEndSample,idxUse(2),exROI+1), 'color',0*[1,1,1],'linewidth',lineWidth);
plot(tvUse, cTracesActFit(1:tvEndSample,idxUse(1),1), 'color','b','linewidth',lineWidth);
plot(tvUse+tvOffset, cTracesActFit(1:tvEndSample,idxUse(2),1), 'color',[0.65,0,0],'linewidth',lineWidth);
xlim([0 21]);
ylim([-0.08 0.8]);
% Scale Bar
scalePosition = [18 0.2 2 0.1];
line(scalePosition(1)+[0 scalePosition(3)],scalePosition(2)*[1,1],'linewidth',lineWidth,'color','k');
line(scalePosition(1)*[1,1],scalePosition(2)+[0 scalePosition(4)],'linewidth',lineWidth,'color','k');
text(scalePosition(1),scalePosition(2),sprintf('%d ms',scalePosition(3)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(scalePosition(1),scalePosition(2),sprintf('%.1f S/cm^2',scalePosition(4)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
% Mark AP Times
line([1 1],[-0.06 -0.02],'color','k','linewidth',lineWidth);
line([1 1]+tvOffset,[-0.06 -0.02],'color','k','linewidth',lineWidth);
% Label
text(1,0.01,'calcium','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom');
set(gca,'visible','off');



exSumWidth = 11.4 / 6;
exSumHeight = vcHeight;

setBackX = [0, -0.07, -0.09, -0.54]; 
xMidpoint = [0.4 1];
xDiff = 0.12;
xPos = [xMidpoint(1)+xDiff*[-1,1] xMidpoint(2)+xDiff*[-1,1]];
xLim = [0 xPos(4)+xDiff];



% Figure 7D
setBackY = -120/60*[2,3,3,5];
figure(4); clf; hold on;
set(gcf,'units','centimeters','position',[25 20 exSumWidth mzpHeight]);
set(gca,'units','centimeters','position',[0.83 0.65 exSumWidth*0.55 mzpHeight*0.78]);
plot(xPos([2,1]),exApAmp(:,:,1),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),exApAmp(1,:,1),'color','b','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos([4,3]),exApAmp(:,:,2),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(4),exApAmp(1,:,2),'color',[0.65,0,0],'marker','.','markersize',markerSize,'linestyle','none');
xlim([xLim(1)+setBackX(2) xLim(2)]);
ylim([-80+setBackY(2) 40]);
xticks = xMidpoint;
xlabels = {'intact','cut'};
for xtidx = 1:length(xticks)
    line(xticks(xtidx)*[1,1],-80 + setBackY([1 2]),'color','k','linewidth',lineWidth);
    text(xticks(xtidx),-80 + setBackY(3),xlabels{xtidx},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Top','Rotation',45);
end
line(xLim(1)*[1,1],[-80 40],'color','k','linewidth',lineWidth);
yticks = -80:40:40;
for yt = yticks
    line(xLim(1)+setBackX([1,2]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),-20,'AP Peak (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off')


% Figure 7E
xTickHeight = -0.88/60;
setBackY = -0.88/60*[2,3,3,5];
figure(5); clf; hold on; 
set(gcf,'units','centimeters','position',[30 20 exSumWidth mzpHeight]);
set(gca,'units','centimeters','position',[0.83 0.65 exSumWidth*0.55 mzpHeight*0.78]);
plot(xPos([2,1]),exCaAmp(:,:,1),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),exCaAmp(1,:,1),'color','b','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos([4,3]),exCaAmp(:,:,2),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(4),exCaAmp(1,:,2),'color',[0.65,0,0],'marker','.','markersize',markerSize,'linestyle','none');
xlim([xLim(1)+setBackX(2) xLim(2)]);
ylim([0+setBackY(2) 0.88]);
for xtidx = 1:length(xticks)
    line(xticks(xtidx)*[1,1],0 + setBackY([1 2]),'color','k','linewidth',lineWidth);
    text(xticks(xtidx),0 + setBackY(3),xlabels{xtidx},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Top','Rotation',45);
end
line(xLim(1)*[1,1],[0 0.88],'color','k','linewidth',lineWidth);
yticks = 0:0.4:0.8;
for yt = yticks
    line(xLim(1)+setBackX([1,2]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),0.88/2,'Peak g_{Ca} (S/cm^2)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off')


% Figure 7F
setBackY = -0.2/60*[2,3,3,5];
figure(6); clf; hold on;
set(gcf,'units','centimeters','position',[35 20 exSumWidth mzpHeight]);
set(gca,'units','centimeters','position',[0.83 0.65 exSumWidth*0.55 mzpHeight*0.78]);
plot(xPos(1), kaDensityAll(silentIDAll==0 & cutExpAll==0), 'color','k','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos(2), kaDensityAll(silentIDAll==1 & cutExpAll==0), 'color','b','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos(3), kaDensityAll(silentIDAll==0 & cutExpAll==1), 'color','k','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos(4), kaDensityAll(silentIDAll==1 & cutExpAll==1), 'color',[0.65,0,0],'marker','.','markersize',markerSize,'linestyle','none');
xlim([xLim(1)+setBackX(2) xLim(2)]);
ylim([0+setBackY(2) 0.2]);
for xtidx = 1:length(xticks)
    line(xticks(xtidx)*[1,1],0 + setBackY([1 2]),'color','k','linewidth',lineWidth);
    text(xticks(xtidx),0 + setBackY(3),xlabels{xtidx},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Top','Rotation',45);
end
line(xLim(1)*[1,1],[0 0.2],'color','k','linewidth',lineWidth);
yticks = 0:0.1:0.2;
for yt = yticks
    line(xLim(1)+setBackX([1,2]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),0.2/2,'S/cm^2','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');


figure(7); clf; hold on;
set(gcf,'units','centimeters','position',[55 10 exSumWidth mzpHeight]);
set(gca,'units','centimeters','position',[0.83 0.65 exSumWidth*0.55 mzpHeight*0.78]);
xlim([xLim(1)+setBackX(2) xLim(2)]);
ylim([0+setBackY(2) 0.2]);
% Create Legend
legPos = [0.2,0.02,0.07,0.05,0.025,0.005];
text(legPos(1),legPos(4)-legPos(6),'High \DeltaCa_{AP}','HorizontalAlignment','Left','Fontsize',fontSize)
text(legPos(1),legPos(5)-legPos(6),'Low \DeltaCa_{AP}','HorizontalAlignment','Left','Fontsize',fontSize)
plot(legPos(2),legPos(4),'marker','.','color','k','markersize',markerSize);
plot(legPos(2)-legPos(3),legPos(5),'marker','.','color','b','markersize',markerSize);
plot(legPos(2)+legPos(3),legPos(5),'marker','.','color',[0.65,0,0],'markersize',markerSize);
set(gca,'visible','off');





tvStartTime = 45;
tvEndTime = 80;
tvOffset = tvEndTime-tvStartTime+3;
tvStartSample = find(tvEpsp>=tvStartTime,1);
tvEndSample = find(tvEpsp>=tvEndTime,1);
tvIdx = tvStartSample:tvEndSample;
tvUse = tvEpsp(tvIdx);
bsWin = [find(tvEpsp>45,1), find(tvEpsp>48,1)];
pkWin = [find(tvEpsp>40,1), find(tvEpsp>70,1)];
baseActExEpspDend = actExEpspDend - mean(actExEpspDend(bsWin(1):bsWin(2),:,:),1);
baseActExEpspSoma = actExEpspSoma - mean(actExEpspSoma(bsWin(1):bsWin(2),:,:),1);
baseSilExEpspDend = silExEpspDend - mean(silExEpspDend(bsWin(1):bsWin(2),:,:),1);
baseSilExEpspSoma = silExEpspSoma - mean(silExEpspSoma(bsWin(1):bsWin(2),:,:),1);
pkActExDend = max(baseActExEpspDend(pkWin(1):pkWin(2),:,:),[],1);
pkActExSoma = max(baseActExEpspSoma(pkWin(1):pkWin(2),:,:),[],1);
pkSilExDend = max(baseSilExEpspDend(pkWin(1):pkWin(2),:,:),[],1);
pkSilExSoma = max(baseSilExEpspSoma(pkWin(1):pkWin(2),:,:),[],1);
pkDendPlot = cat(1, pkSilExDend, pkActExDend);
pkSomaPlot = cat(1, pkSilExSoma, pkActExSoma);
epspWidth = 11.4 * 2/5;
epspHeight = mzpHeight / 2;
xLim = [-18 18];
yLim = [-5 29];
schematicWidth = diff(xLim)/diff(yLim) * epspHeight;
traceWidth = 1.785;
sumWidth = 1.68;

% Injection Pipette Schematic
xCoord = [0 4 4]; 
yCoord = [0 1 -1];
patchCoord = [xCoord; yCoord];
matRotate = @(theta) [cosd(theta), -sind(theta); sind(theta), cosd(theta)];
plotCircleAngle = 0:0.01:2*pi;
% Generic Features
radiusSoma = 3;
radiusROI = 0.7;
lengthFirst = 10;
lineWidth = 0.5;
markerLineWidth = 0.2;
% Apical elaboration parameters
heightSecond = 15;
lengthApicals = 8;
secondAngle = 0; % degrees off center
% Sister branches parameters
positionROI = 5;



% Figure 7G
figure(8); clf; hold on;
set(gcf,'units','centimeters','position',[40 25 schematicWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0 schematicWidth epspHeight])
% Plot recording/injecting electrodes
dendTrode = matRotate(45)*patchCoord;
patch(-positionROI*sind(secondAngle)+dendTrode(1,:), (radiusSoma+lengthFirst+positionROI)+dendTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(0.5-positionROI*sind(secondAngle)+mean(dendTrode(1,2:3)),0.4+(radiusSoma+lengthFirst+positionROI)+mean(dendTrode(2,2:3)),'v_{rec}','fontsize',fontSize,'HorizontalAlignment','Left','VerticalALignment','Middle');
dendTrode = matRotate(180)*patchCoord;
patch(-positionROI*sind(secondAngle)+dendTrode(1,:), (radiusSoma+lengthFirst+positionROI)+dendTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(-0.5-positionROI*sind(secondAngle)+mean(dendTrode(1,2:3)),(radiusSoma+lengthFirst+positionROI)+mean(dendTrode(2,2:3)),'g_{inj}','fontsize',fontSize,'HorizontalAlignment','Right','VerticalALignment','Middle');
% Plot sister branches
patch(radiusSoma*cos(plotCircleAngle),radiusSoma*sin(plotCircleAngle),'k');
line([0 0],[radiusSoma radiusSoma+lengthFirst],'color','k','linewidth',lineWidth);
line([0 -heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
line([0  heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
% Plot ROIs
roiX = positionROI*sind(secondAngle)+radiusROI*cos(plotCircleAngle);
roiY = radiusSoma + lengthFirst + positionROI+radiusROI*sin(plotCircleAngle);
patch(-roiX,roiY,'b','LineWidth',markerLineWidth);
patch(roiX,roiY,'k','LineWidth',markerLineWidth);
xlim(xLim);
ylim(yLim);
set(gca,'visible','off');
figure(9); clf; hold on; 
set(gcf,'units','centimeters','position',[45 25 traceWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0.1 traceWidth epspHeight])
shadedErrorBar(tvUse, mean(baseActExEpspDend(tvIdx,:,1),2), std(baseActExEpspDend(tvIdx,:,1),1,2),{'color','k','linewidth',lineWidth},1);
shadedErrorBar(tvUse, mean(baseSilExEpspDend(tvIdx,:,1),2), std(baseSilExEpspDend(tvIdx,:,1),1,2),{'color','b','linewidth',lineWidth},1);
xlim([43 82]);
ylim([-0.5 6]);
line([105 105],[3.5 4.5],'color','k','linewidth',lineWidth);
line([105 115],[3.5 3.5],'color','k','linewidth',lineWidth);
text(105,3.5,'10 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(104.5,3.5,'1 mV','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
line([50 50],[-0.5 -0.2],'color','k','linewidth',lineWidth);
set(gca,'visible','off');
setBackX = [-0.1 -0.15 -0.2 -0.4];
xPos = [0.1, 0.5];
figure(10); clf; hold on;
set(gcf,'units','centimeters','position',[50 25 sumWidth epspHeight]);
set(gca,'units','centimeters','position',[0.6 0.2 sumWidth*0.9 epspHeight*0.85]);
plot(xPos([2 1]),pkDendPlot(:,:,1),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),pkDendPlot(1,:,1),'color','b','linestyle','none','marker','.','markersize',markerSize);
xLim = [0 1.5];
xlim(xLim + [setBackX(2),0]);
ylim([0 9]);
line(xLim(1)+setBackX([1,1]),[0 10],'color','k','linewidth',lineWidth);
yticks = 0:4:8;
for yt = yticks
    line(xLim(1)+setBackX([2 1]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),4.5,'EPSP (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');


% Injection Pipette Schematic
xCoord = [0 4 4]; 
yCoord = [0 1 -1];
patchCoord = [xCoord; yCoord];
matRotate = @(theta) [cosd(theta), -sind(theta); sind(theta), cosd(theta)];
plotCircleAngle = 0:0.01:2*pi;
% Generic Features
radiusSoma = 3;
radiusROI = 0.7;
lengthFirst = 10;
lineWidth = 0.5;
markerLineWidth = 0.2;
% Apical elaboration parameters
heightSecond = 15;
lengthApicals = 8;
secondAngle = 0; % degrees off center
% Sister branches parameters
positionROI = 5;
xLim = [-18 18];
yLim = [-5 29];

% Figure 7H
figure(11); clf; hold on;
set(gcf,'units','centimeters','position',[40 18 schematicWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0 schematicWidth epspHeight])
% Plot recording/injecting electrodes
somaTrode = matRotate(45)*patchCoord;
patch(radiusSoma*cosd(45)+somaTrode(1,:), radiusSoma*sind(45)+somaTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(0.5+radiusSoma*cosd(45)+mean(somaTrode(1,2:3)),0.5+radiusSoma*sind(45)+mean(somaTrode(2,2:3)),'v_{rec}','fontsize',fontSize,'HorizontalAlignment','Left','VerticalALignment','Middle');
dendTrode = matRotate(180)*patchCoord;
patch(-positionROI*sind(secondAngle)+dendTrode(1,:), (radiusSoma+lengthFirst+positionROI)+dendTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(-0.5-positionROI*sind(secondAngle)+mean(dendTrode(1,2:3)),(radiusSoma+lengthFirst+positionROI)+mean(dendTrode(2,2:3)),'g_{inj}','fontsize',fontSize,'HorizontalAlignment','Right','VerticalALignment','Middle');
% Plot sister branches
patch(radiusSoma*cos(plotCircleAngle),radiusSoma*sin(plotCircleAngle),'k');
line([0 0],[radiusSoma radiusSoma+lengthFirst],'color','k','linewidth',lineWidth);
line([0 -heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
line([0  heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
% Plot ROIs
roiX = positionROI*sind(secondAngle)+radiusROI*cos(plotCircleAngle);
roiY = radiusSoma + lengthFirst + positionROI+radiusROI*sin(plotCircleAngle);
patch(-roiX,roiY,'b','LineWidth',markerLineWidth);
xlim(xLim);
ylim(yLim);
set(gca,'visible','off');
figure(12); clf; hold on; 
set(gcf,'units','centimeters','position',[45 18 traceWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0.1 traceWidth epspHeight])
shadedErrorBar(tvUse, mean(baseActExEpspSoma(tvIdx,:,1),2), std(baseActExEpspSoma(tvIdx,:,1),1,2),{'color','k','linewidth',lineWidth},1);
shadedErrorBar(tvUse, mean(baseSilExEpspSoma(tvIdx,:,1),2), std(baseSilExEpspSoma(tvIdx,:,1),1,2),{'color','b','linewidth',lineWidth},1);
xlim([43 82]);
ylim([-0.5 6]);
line([65 65],[2.5 3.5],'color','k','linewidth',lineWidth);
line([65 75],[2.5 2.5],'color','k','linewidth',lineWidth);
text(65,2.5,'10 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(65,2.5,'1 mV','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
line([50 50],[-0.5 -0.2],'color','k','linewidth',lineWidth);
set(gca,'visible','off');
setBackX = [-0.1 -0.15 -0.2 -0.4];
xPos = [0.1, 0.5];
figure(13); clf; hold on;
set(gcf,'units','centimeters','position',[50 18 sumWidth epspHeight]);
set(gca,'units','centimeters','position',[0.6 0.2 sumWidth*0.9 epspHeight*0.85]);
plot(xPos([2 1]),pkSomaPlot(:,:,1),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),pkSomaPlot(1,:,1),'color','b','linestyle','none','marker','.','markersize',markerSize);
xLim = [0 1.5];
xlim(xLim + [setBackX(2),0]);
ylim([0 9]);
line(xLim(1)+setBackX([1,1]),[0 10],'color','k','linewidth',lineWidth);
yticks = 0:4:8;
for yt = yticks
    line(xLim(1)+setBackX([2 1]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),4.5,'EPSP (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');








%% == Figure 7 - Figure supplement 2 ==

close all

data4AP = allData.data4AP;
tv = data4AP.matTV;
idxROI = data4AP.matIdxROI;
idxActFit = idxROI~=0;
idxROI_ActFit = idxROI(idxActFit);
vTracesActFit = data4AP.matVoltage(:,idxActFit,:);
cTracesActFit = data4AP.matCalcium(:,idxActFit,:);
apAmpActFit = data4AP.matApAmp(idxActFit,:);
caAmpActFit = data4AP.matCaAmp(idxActFit,:);
iresActFit = data4AP.matIres(idxActFit,:);
epspDendActFit = data4AP.epspAmpDend(idxActFit,:);
epspSomaActFit = data4AP.epspAmpSoma(idxActFit,:);
cellID_ActFit = data4AP.matCellID(idxActFit);
apply4AP_ActFit = data4AP.matApply4AP(idxActFit);

tvEpsp = data4AP.tvEpsp(1,:);
vEpspDendActFit = data4AP.epspDendTraces(:,idxActFit,:);
vEpspSomaActFit = data4AP.epspSomaTraces(:,idxActFit,:);

NT = size(vTracesActFit,1);
numFits = size(vTracesActFit, 2)/2;

maxROI = size(vTracesActFit, 3);

actExVoltage = zeros(NT,numFits,2);
silExVoltage = zeros(NT,numFits,2);
silExVoltage(:,:,1) = vTracesActFit(:,apply4AP_ActFit==0,1);
silExVoltage(:,:,2) = vTracesActFit(:,apply4AP_ActFit==1,1);
actExCalcium = zeros(NT,numFits,2);
silExCalcium = zeros(NT,numFits,2);
silExCalcium(:,:,1) = cTracesActFit(:,apply4AP_ActFit==0,1);
silExCalcium(:,:,2) = cTracesActFit(:,apply4AP_ActFit==1,1);
exApAmp = zeros(2,numFits,2);
exCaAmp = zeros(2,numFits,2);
exIres = zeros(2,numFits,2);
exEpspDend = zeros(2,numFits,2);
exEpspSoma = zeros(2,numFits,2);
exApAmp(1,:,1) = apAmpActFit(apply4AP_ActFit==0,1);
exCaAmp(1,:,1) = caAmpActFit(apply4AP_ActFit==0,1);
exIres(1,:,1) = iresActFit(apply4AP_ActFit==0,1);
exEpspDend(1,:,1) = epspDendActFit(apply4AP_ActFit==0,1);
exEpspSoma(1,:,1) = epspSomaActFit(apply4AP_ActFit==0,1);
exApAmp(1,:,2) = apAmpActFit(apply4AP_ActFit==1,1);
exCaAmp(1,:,2) = caAmpActFit(apply4AP_ActFit==1,1);
exIres(1,:,2) = iresActFit(apply4AP_ActFit==1,1);
exEpspDend(1,:,2) = epspDendActFit(apply4AP_ActFit==1,1);
exEpspSoma(1,:,2) = epspSomaActFit(apply4AP_ActFit==1,1);
epspNT = length(tvEpsp);
actExEpspDend = zeros(epspNT,numFits,2);
silExEpspDend = zeros(epspNT,numFits,2);
actExEpspSoma = zeros(epspNT,numFits,2);
silExEpspSoma = zeros(epspNT,numFits,2);
silExEpspDend(:,:,1) = vEpspDendActFit(:,apply4AP_ActFit==0,1);
silExEpspDend(:,:,2) = vEpspDendActFit(:,apply4AP_ActFit==1,1);
silExEpspSoma(:,:,1) = vEpspSomaActFit(:,apply4AP_ActFit==0,1);
silExEpspSoma(:,:,2) = vEpspSomaActFit(:,apply4AP_ActFit==1,1);

idxCtl = find(apply4AP_ActFit==0);
idx4AP = find(apply4AP_ActFit);
for nf = 1:numFits
    actExVoltage(:,nf,1) = vTracesActFit(:,idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    actExCalcium(:,nf,1) = cTracesActFit(:,idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    exApAmp(2,nf,1) = apAmpActFit(idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    exCaAmp(2,nf,1) = caAmpActFit(idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    exIres(2,nf,1) = iresActFit(idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    exEpspDend(2,nf,1) = actExEpspDend(idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    exEpspSoma(2,nf,1) = actExEpspSoma(idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    
    actExEpspDend(:,nf,1) = vEpspDendActFit(:,idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
    actExEpspSoma(:,nf,1) = vEpspSomaActFit(:,idxCtl(nf),idxROI_ActFit(idxCtl(nf))+1);
end
for nf = 1:numFits
    actExVoltage(:,nf,2) = vTracesActFit(:,idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    actExCalcium(:,nf,2) = cTracesActFit(:,idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    exApAmp(2,nf,2) = apAmpActFit(idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    exCaAmp(2,nf,2) = caAmpActFit(idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    exIres(2,nf,2) = iresActFit(idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    exEpspDend(2,nf,2) = actExEpspDend(idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    exEpspSoma(2,nf,2) = actExEpspSoma(idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    
    actExEpspDend(:,nf,2) = vEpspDendActFit(:,idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
    actExEpspSoma(:,nf,2) = vEpspSomaActFit(:,idx4AP(nf),idxROI_ActFit(idx4AP(nf))+1);
end

mzpFigureWidth = 4.12;
mzpHeight = 3.4; 
exCell = 1;
exROI = 1;
idxCell = find(cellID_ActFit==exCell);
idxUse = idxCell((0:1) + 2*(exROI-1)+1); % First will be intact, second is cut

tvEndTime = 10;
tvOffset = tvEndTime+1;
tvEndSample = find(tv>=tvEndTime,1);
tvUse = tv(1:tvEndSample);

vcWidth = 11.4 - mzpFigureWidth;
vcHeight = mzpHeight/2;

% Figure 7 - figure supplement 2A
figure(1); clf; hold on; 
set(gcf,'units','centimeters','position',[30 30 vcWidth vcHeight]);
set(gca,'units','centimeters','position',[0.1 0.05 vcWidth*0.93 vcHeight]);
plot(tvUse, vTracesActFit(1:tvEndSample,idxUse(1),exROI+1), 'color','k','linewidth',lineWidth);
plot(tvUse+tvOffset, vTracesActFit(1:tvEndSample,idxUse(2),exROI+1), 'color',0*[1,1,1],'linewidth',lineWidth);
plot(tvUse, vTracesActFit(1:tvEndSample,idxUse(1),1), 'color','b','linewidth',lineWidth);
plot(tvUse+tvOffset, vTracesActFit(1:tvEndSample,idxUse(2),1), 'color',[0,0.65,0],'linewidth',lineWidth);
xlim([0 21]);
ylim([-87 60]);
% Scale Bar
scalePosition = [18 -55 2 10];
line(scalePosition(1)+[0 scalePosition(3)],scalePosition(2)*[1,1],'linewidth',lineWidth,'color','k');
line(scalePosition(1)*[1,1],scalePosition(2)+[0 scalePosition(4)],'linewidth',lineWidth,'color','k');
text(scalePosition(1),scalePosition(2),sprintf('%d ms',scalePosition(3)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(scalePosition(1),scalePosition(2),sprintf('%d mV',scalePosition(4)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
% Mark AP Times
line([1 1],[-85 -80],'color','k','linewidth',lineWidth);
line([1 1]+tvOffset,[-85 -80],'color','k','linewidth',lineWidth);
% Label
text(1,-71,'voltage','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom');
set(gca,'visible','off');

% Figure 7 - figure supplement 2B
figure(2); clf; hold on;
set(gcf,'units','centimeters','position',[30 23 vcWidth vcHeight]);
set(gca,'units','centimeters','position',[0.1 0.05 vcWidth*0.93 vcHeight]);
plot(tvUse, cTracesActFit(1:tvEndSample,idxUse(1),exROI+1), 'color','k','linewidth',lineWidth);
plot(tvUse+tvOffset, cTracesActFit(1:tvEndSample,idxUse(2),exROI+1), 'color',0*[1,1,1],'linewidth',lineWidth);
plot(tvUse, cTracesActFit(1:tvEndSample,idxUse(1),1), 'color','b','linewidth',lineWidth);
plot(tvUse+tvOffset, cTracesActFit(1:tvEndSample,idxUse(2),1), 'color',[0,0.65,0],'linewidth',lineWidth);
xlim([0 21]);
ylim([-0.08 0.9]);
% Scale Bar
scalePosition = [18 0.2 2 0.1];
line(scalePosition(1)+[0 scalePosition(3)],scalePosition(2)*[1,1],'linewidth',lineWidth,'color','k');
line(scalePosition(1)*[1,1],scalePosition(2)+[0 scalePosition(4)],'linewidth',lineWidth,'color','k');
text(scalePosition(1),scalePosition(2),sprintf('%d ms',scalePosition(3)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(scalePosition(1),scalePosition(2),sprintf('%.1f S/cm^2',scalePosition(4)),'Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
% Mark AP Times
line([1 1],[-0.06 -0.02],'color','k','linewidth',lineWidth);
line([1 1]+tvOffset,[-0.06 -0.02],'color','k','linewidth',lineWidth);
% Label
text(1,0.01,'calcium','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom');
set(gca,'visible','off');



exSumWidth = 11.4 / 6;
exSumHeight = vcHeight;

setBackX = [0, -0.07, -0.09, -0.54]; 
xMidpoint = [0.4 1];
xDiff = 0.12;
xPos = [xMidpoint(1)+xDiff*[-1,1] xMidpoint(2)+xDiff*[-1,1]];
xLim = [0 xPos(4)+xDiff];


% Figure 7 - figure supplement 2C
setBackY = -120/60*[2,3,3,5];
figure(3); clf; hold on;
set(gcf,'units','centimeters','position',[40 23 exSumWidth mzpHeight]);
set(gca,'units','centimeters','position',[0.83 0.65 exSumWidth*0.55 mzpHeight*0.78]);
plot(xPos([2,1]),exApAmp(:,:,1),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),exApAmp(1,:,1),'color','b','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos([4,3]),exApAmp(:,:,2),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(4),exApAmp(1,:,2),'color',[0,0.65,0],'marker','.','markersize',markerSize,'linestyle','none');
xlim([xLim(1)+setBackX(2) xLim(2)]);
ylim([-80+setBackY(2) 60]);
xticks = xMidpoint;
xlabels = {'control','\DeltaKvA'};
for xtidx = 1:length(xticks)
    line(xticks(xtidx)*[1,1],-80 + setBackY([1 2]),'color','k','linewidth',lineWidth);
    text(xticks(xtidx),-80 + setBackY(3),xlabels{xtidx},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Top','Rotation',45);
end
line(xLim(1)*[1,1],[-80 60],'color','k','linewidth',lineWidth);
yticks = -80:40:40;
for yt = yticks
    line(xLim(1)+setBackX([1,2]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),-20,'AP Peak (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off')


% Figure 7 - figure supplement 2D
xTickHeight = -0.88/60;
setBackY = -0.88/60*[2,3,3,5];
figure(4); clf; hold on; 
set(gcf,'units','centimeters','position',[45 23 exSumWidth mzpHeight]);
set(gca,'units','centimeters','position',[0.83 0.65 exSumWidth*0.55 mzpHeight*0.78]);
plot(xPos([2,1]),exCaAmp(:,:,1),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),exCaAmp(1,:,1),'color','b','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos([4,3]),exCaAmp(:,:,2),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(4),exCaAmp(1,:,2),'color',[0,0.65,0],'marker','.','markersize',markerSize,'linestyle','none');
xlim([xLim(1)+setBackX(2) xLim(2)]);
ylim([0+setBackY(2) 0.9]);
for xtidx = 1:length(xticks)
    line(xticks(xtidx)*[1,1],0 + setBackY([1 2]),'color','k','linewidth',lineWidth);
    text(xticks(xtidx),0 + setBackY(3),xlabels{xtidx},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Top','Rotation',45);
end
line(xLim(1)*[1,1],[0 0.9],'color','k','linewidth',lineWidth);
yticks = 0:0.4:0.8;
for yt = yticks
    line(xLim(1)+setBackX([1,2]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),0.88/2,'Peak g_{Ca} (S/cm^2)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off')




cutColor = [0.65,0,0];
tvStartTime = 45;
tvEndTime = 80;
tvOffset = tvEndTime-tvStartTime+3;
tvStartSample = find(tvEpsp>=tvStartTime,1);
tvEndSample = find(tvEpsp>=tvEndTime,1);
tvIdx = tvStartSample:tvEndSample;
tvUse = tvEpsp(tvIdx);
bsWin = [find(tvEpsp>45,1), find(tvEpsp>48,1)];
pkWin = [find(tvEpsp>40,1), find(tvEpsp>70,1)];
baseActExEpspDend = actExEpspDend - mean(actExEpspDend(bsWin(1):bsWin(2),:,:),1);
baseActExEpspSoma = actExEpspSoma - mean(actExEpspSoma(bsWin(1):bsWin(2),:,:),1);
baseSilExEpspDend = silExEpspDend - mean(silExEpspDend(bsWin(1):bsWin(2),:,:),1);
baseSilExEpspSoma = silExEpspSoma - mean(silExEpspSoma(bsWin(1):bsWin(2),:,:),1);
pkActExDend = max(baseActExEpspDend(pkWin(1):pkWin(2),:,:),[],1);
pkActExSoma = max(baseActExEpspSoma(pkWin(1):pkWin(2),:,:),[],1);
pkSilExDend = max(baseSilExEpspDend(pkWin(1):pkWin(2),:,:),[],1);
pkSilExSoma = max(baseSilExEpspSoma(pkWin(1):pkWin(2),:,:),[],1);
pkDendPlot = cat(1, pkSilExDend, pkActExDend);
pkSomaPlot = cat(1, pkSilExSoma, pkActExSoma);
epspWidth = 11.4 * 2/5;
epspHeight = mzpHeight / 2;

xLim = [-18 18];
yLim = [-5 29];

schematicWidth = diff(xLim)/diff(yLim) * epspHeight;
traceWidth = 1.785;
sumWidth = 1.68;

% Injection Pipette Schematic
xCoord = [0 4 4]; 
yCoord = [0 1 -1];
patchCoord = [xCoord; yCoord];
matRotate = @(theta) [cosd(theta), -sind(theta); sind(theta), cosd(theta)];
plotCircleAngle = 0:0.01:2*pi;
% Generic Features
radiusSoma = 3;
radiusROI = 0.7;
lengthFirst = 10;
lineWidth = 0.5;
markerLineWidth = 0.2;
% Apical elaboration parameters
heightSecond = 15;
lengthApicals = 8;
secondAngle = 0; % degrees off center
% Sister branches parameters
positionROI = 5;




% Figure 7 - figure supplement 2E
figure(5); clf; hold on;
set(gcf,'units','centimeters','position',[50 30 schematicWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0 schematicWidth epspHeight])
% Plot recording/injecting electrodes
dendTrode = matRotate(45)*patchCoord;
patch(-positionROI*sind(secondAngle)+dendTrode(1,:), (radiusSoma+lengthFirst+positionROI)+dendTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(0.5-positionROI*sind(secondAngle)+mean(dendTrode(1,2:3)),0.4+(radiusSoma+lengthFirst+positionROI)+mean(dendTrode(2,2:3)),'v_{rec}','fontsize',fontSize,'HorizontalAlignment','Left','VerticalALignment','Middle');
dendTrode = matRotate(180)*patchCoord;
patch(-positionROI*sind(secondAngle)+dendTrode(1,:), (radiusSoma+lengthFirst+positionROI)+dendTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(-0.5-positionROI*sind(secondAngle)+mean(dendTrode(1,2:3)),(radiusSoma+lengthFirst+positionROI)+mean(dendTrode(2,2:3)),'g_{inj}','fontsize',fontSize,'HorizontalAlignment','Right','VerticalALignment','Middle');
% Plot sister branches
patch(radiusSoma*cos(plotCircleAngle),radiusSoma*sin(plotCircleAngle),'k');
line([0 0],[radiusSoma radiusSoma+lengthFirst],'color','k','linewidth',lineWidth);
line([0 -heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
line([0  heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
% Plot ROIs
roiX = positionROI*sind(secondAngle)+radiusROI*cos(plotCircleAngle);
roiY = radiusSoma + lengthFirst + positionROI+radiusROI*sin(plotCircleAngle);
patch(-roiX,roiY,'b','LineWidth',markerLineWidth);
patch(roiX,roiY,'k','LineWidth',markerLineWidth);
xlim(xLim);
ylim(yLim);
set(gca,'visible','off');
figure(6); clf; hold on; 
set(gcf,'units','centimeters','position',[55 30 traceWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0.1 traceWidth epspHeight])
shadedErrorBar(tvUse, mean(baseActExEpspDend(tvIdx,:,2),2), std(baseActExEpspDend(tvIdx,:,2),1,2),{'color','k','linewidth',lineWidth},1);
shadedErrorBar(tvUse, mean(baseSilExEpspDend(tvIdx,:,2),2), std(baseSilExEpspDend(tvIdx,:,2),1,2),{'color',cutColor,'linewidth',lineWidth},1);
xlim([43 82]);
ylim([-0.5 6]);
line([105 105],[3.5 4.5],'color','k','linewidth',lineWidth);
line([105 115],[3.5 3.5],'color','k','linewidth',lineWidth);
text(105,3.5,'10 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(104.5,3.5,'1 mV','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
line([50 50],[-0.5 -0.2],'color','k','linewidth',lineWidth);
set(gca,'visible','off');
setBackX = [-0.1 -0.15 -0.2 -0.4];
xPos = [0.1, 0.5];
figure(7); clf; hold on;
set(gcf,'units','centimeters','position',[60 30 sumWidth epspHeight]);
set(gca,'units','centimeters','position',[0.6 0.2 sumWidth*0.9 epspHeight*0.85]);
plot(xPos([2 1]),pkDendPlot(:,:,2),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),pkDendPlot(1,:,2),'color',cutColor,'linestyle','none','marker','.','markersize',markerSize);
xLim = [0 1.5];
xlim(xLim + [setBackX(2),0]);
ylim([0 9]);
line(xLim(1)+setBackX([1,1]),[0 10],'color','k','linewidth',lineWidth);
yticks = 0:4:8;
for yt = yticks
    line(xLim(1)+setBackX([2 1]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),4.5,'EPSP (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');




xCoord = [0 4 4]; 
yCoord = [0 1 -1];
patchCoord = [xCoord; yCoord];
matRotate = @(theta) [cosd(theta), -sind(theta); sind(theta), cosd(theta)];
plotCircleAngle = 0:0.01:2*pi;
% Generic Features
radiusSoma = 3;
radiusROI = 0.7;
lengthFirst = 10;
lineWidth = 0.5;
markerLineWidth = 0.2;
% Apical elaboration parameters
heightSecond = 15;
lengthApicals = 8;
secondAngle = 0; % degrees off center
% Sister branches parameters
positionROI = 5;

xLim = [-18 18];
yLim = [-5 29];

% Figure 7 - figure supplement 2F
figure(8); clf; hold on;
set(gcf,'units','centimeters','position',[50 23 schematicWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0 schematicWidth epspHeight])
% Plot recording/injecting electrodes
somaTrode = matRotate(45)*patchCoord;
patch(radiusSoma*cosd(45)+somaTrode(1,:), radiusSoma*sind(45)+somaTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(0.5+radiusSoma*cosd(45)+mean(somaTrode(1,2:3)),0.5+radiusSoma*sind(45)+mean(somaTrode(2,2:3)),'v_{rec}','fontsize',fontSize,'HorizontalAlignment','Left','VerticalALignment','Middle');
dendTrode = matRotate(180)*patchCoord;
patch(-positionROI*sind(secondAngle)+dendTrode(1,:), (radiusSoma+lengthFirst+positionROI)+dendTrode(2,:),'k','EdgeColor','k','Linewidth',lineWidth,'FaceColor',0.5*[1,1,1]);
text(-0.5-positionROI*sind(secondAngle)+mean(dendTrode(1,2:3)),(radiusSoma+lengthFirst+positionROI)+mean(dendTrode(2,2:3)),'g_{inj}','fontsize',fontSize,'HorizontalAlignment','Right','VerticalALignment','Middle');
% Plot sister branches
patch(radiusSoma*cos(plotCircleAngle),radiusSoma*sin(plotCircleAngle),'k');
line([0 0],[radiusSoma radiusSoma+lengthFirst],'color','k','linewidth',lineWidth);
line([0 -heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
line([0  heightSecond*sind(secondAngle)],radiusSoma+lengthFirst + [0 heightSecond],'color','k','linewidth',lineWidth);
% Plot ROIs
roiX = positionROI*sind(secondAngle)+radiusROI*cos(plotCircleAngle);
roiY = radiusSoma + lengthFirst + positionROI+radiusROI*sin(plotCircleAngle);
patch(-roiX,roiY,'b','LineWidth',markerLineWidth);
xlim(xLim);
ylim(yLim);
set(gca,'visible','off');
figure(9); clf; hold on; 
set(gcf,'units','centimeters','position',[55 23 traceWidth epspHeight]);
set(gca,'units','centimeters','position',[0 0.1 traceWidth epspHeight])
shadedErrorBar(tvUse, mean(baseActExEpspSoma(tvIdx,:,2),2), std(baseActExEpspSoma(tvIdx,:,2),1,2),{'color','k','linewidth',lineWidth},1);
shadedErrorBar(tvUse, mean(baseSilExEpspSoma(tvIdx,:,2),2), std(baseSilExEpspSoma(tvIdx,:,2),1,2),{'color',cutColor,'linewidth',lineWidth},1);
xlim([43 82]);
ylim([-0.5 6]);
line([65 65],[2.5 3.5],'color','k','linewidth',lineWidth);
line([65 75],[2.5 2.5],'color','k','linewidth',lineWidth);
text(65,2.5,'10 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(65,2.5,'1 mV','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
line([50 50],[-0.5 -0.2],'color','k','linewidth',lineWidth);
set(gca,'visible','off');
setBackX = [-0.1 -0.15 -0.2 -0.4];
xPos = [0.1, 0.5];
figure(10); clf; hold on;
set(gcf,'units','centimeters','position',[60 23 sumWidth epspHeight]);
set(gca,'units','centimeters','position',[0.6 0.2 sumWidth*0.9 epspHeight*0.85]);
plot(xPos([2 1]),pkSomaPlot(:,:,2),'color','k','linewidth',lineWidth,'marker','.','markersize',markerSize);
plot(xPos(2),pkSomaPlot(1,:,2),'color',cutColor,'linestyle','none','marker','.','markersize',markerSize);
xLim = [0 1.5];
xlim(xLim + [setBackX(2),0]);
ylim([0 9]);
line(xLim(1)+setBackX([1,1]),[0 10],'color','k','linewidth',lineWidth);
yticks = 0:4:8;
for yt = yticks
    line(xLim(1)+setBackX([2 1]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackX(3),yt,num2str(yt),'fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),4.5,'EPSP (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');










